package com.base.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.base.common.core.page.TableDataInfo;
import com.base.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.base.system.domain.bo.TSysMenuBo;
import com.base.system.domain.vo.TSysMenuVo;
import com.base.system.mapper.TSysMenuMapper;
import com.base.system.service.ITSysMenuService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.base.common.utils.StringUtils;
import com.base.system.domain.TSysMenu;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 权限菜单Service业务层处理
 *
 * @author base
 * @date 2023-10-18
 */
@RequiredArgsConstructor
@Service
public class TSysMenuServiceImpl implements ITSysMenuService {

    private final TSysMenuMapper baseMapper;

    /**
     * 查询权限菜单
     */
    @Override
    public TSysMenuVo queryById(String menuId){
        return baseMapper.selectVoById(menuId);
    }

    /**
     * 查询权限菜单列表
     */
    @Override
    public TableDataInfo<TSysMenuVo> queryPageList(TSysMenuBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TSysMenu> lqw = buildQueryWrapper(bo);
        Page<TSysMenuVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询权限菜单列表
     */
    @Override
    public List<TSysMenuVo> queryList(TSysMenuBo bo) {
        LambdaQueryWrapper<TSysMenu> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TSysMenu> buildQueryWrapper(TSysMenuBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TSysMenu> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getMenuName()), TSysMenu::getMenuName, bo.getMenuName());
        lqw.eq(StringUtils.isNotBlank(bo.getTitle()), TSysMenu::getTitle, bo.getTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getParentId()), TSysMenu::getParentId, bo.getParentId());
        lqw.eq(StringUtils.isNotBlank(bo.getMenuType()), TSysMenu::getMenuType, bo.getMenuType());
        return lqw;
    }

    /**
     * 新增权限菜单
     */
    @Override
    public Boolean insertByBo(TSysMenuBo bo) {
        TSysMenu add = BeanUtil.toBean(bo, TSysMenu.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setMenuId(add.getMenuId());
        }
        return flag;
    }

    /**
     * 修改权限菜单
     */
    @Override
    public Boolean updateByBo(TSysMenuBo bo) {
        TSysMenu update = BeanUtil.toBean(bo, TSysMenu.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TSysMenu entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除权限菜单
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
